{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>School</th>\n",
       "      <th>Class</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Address</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Math</th>\n",
       "      <th>Physics</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1101</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_1</td>\n",
       "      <td>M</td>\n",
       "      <td>street_1</td>\n",
       "      <td>173</td>\n",
       "      <td>63</td>\n",
       "      <td>34.0</td>\n",
       "      <td>A+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1102</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_1</td>\n",
       "      <td>F</td>\n",
       "      <td>street_2</td>\n",
       "      <td>192</td>\n",
       "      <td>73</td>\n",
       "      <td>32.5</td>\n",
       "      <td>B+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1103</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_1</td>\n",
       "      <td>M</td>\n",
       "      <td>street_2</td>\n",
       "      <td>186</td>\n",
       "      <td>82</td>\n",
       "      <td>87.2</td>\n",
       "      <td>B+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1104</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_1</td>\n",
       "      <td>F</td>\n",
       "      <td>street_2</td>\n",
       "      <td>167</td>\n",
       "      <td>81</td>\n",
       "      <td>80.4</td>\n",
       "      <td>B-</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1105</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_1</td>\n",
       "      <td>F</td>\n",
       "      <td>street_4</td>\n",
       "      <td>159</td>\n",
       "      <td>64</td>\n",
       "      <td>84.8</td>\n",
       "      <td>B+</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1102    S_1   C_1      F  street_2     192      73  32.5      B+\n",
       "1103    S_1   C_1      M  street_2     186      82  87.2      B+\n",
       "1104    S_1   C_1      F  street_2     167      81  80.4      B-\n",
       "1105    S_1   C_1      F  street_4     159      64  84.8      B+"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.read_csv('data/table.csv',index_col = 'ID')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "split 将数据拆分为若干组\n",
    "apply 对每一组独立的使用函数\n",
    "combine 每一组的结果组合成某一类数据结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>School</th>\n",
       "      <th>Class</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Address</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Math</th>\n",
       "      <th>Physics</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1101</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_1</td>\n",
       "      <td>M</td>\n",
       "      <td>street_1</td>\n",
       "      <td>173</td>\n",
       "      <td>63</td>\n",
       "      <td>34.0</td>\n",
       "      <td>A+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1102</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_1</td>\n",
       "      <td>F</td>\n",
       "      <td>street_2</td>\n",
       "      <td>192</td>\n",
       "      <td>73</td>\n",
       "      <td>32.5</td>\n",
       "      <td>B+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1103</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_1</td>\n",
       "      <td>M</td>\n",
       "      <td>street_2</td>\n",
       "      <td>186</td>\n",
       "      <td>82</td>\n",
       "      <td>87.2</td>\n",
       "      <td>B+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1104</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_1</td>\n",
       "      <td>F</td>\n",
       "      <td>street_2</td>\n",
       "      <td>167</td>\n",
       "      <td>81</td>\n",
       "      <td>80.4</td>\n",
       "      <td>B-</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1105</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_1</td>\n",
       "      <td>F</td>\n",
       "      <td>street_4</td>\n",
       "      <td>159</td>\n",
       "      <td>64</td>\n",
       "      <td>84.8</td>\n",
       "      <td>B+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1201</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_2</td>\n",
       "      <td>M</td>\n",
       "      <td>street_5</td>\n",
       "      <td>188</td>\n",
       "      <td>68</td>\n",
       "      <td>97.0</td>\n",
       "      <td>A-</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1202</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_2</td>\n",
       "      <td>F</td>\n",
       "      <td>street_4</td>\n",
       "      <td>176</td>\n",
       "      <td>94</td>\n",
       "      <td>63.5</td>\n",
       "      <td>B-</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1203</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_2</td>\n",
       "      <td>M</td>\n",
       "      <td>street_6</td>\n",
       "      <td>160</td>\n",
       "      <td>53</td>\n",
       "      <td>58.8</td>\n",
       "      <td>A+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1204</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_2</td>\n",
       "      <td>F</td>\n",
       "      <td>street_5</td>\n",
       "      <td>162</td>\n",
       "      <td>63</td>\n",
       "      <td>33.8</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1205</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_2</td>\n",
       "      <td>F</td>\n",
       "      <td>street_6</td>\n",
       "      <td>167</td>\n",
       "      <td>63</td>\n",
       "      <td>68.4</td>\n",
       "      <td>B-</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1301</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_3</td>\n",
       "      <td>M</td>\n",
       "      <td>street_4</td>\n",
       "      <td>161</td>\n",
       "      <td>68</td>\n",
       "      <td>31.5</td>\n",
       "      <td>B+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1302</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_3</td>\n",
       "      <td>F</td>\n",
       "      <td>street_1</td>\n",
       "      <td>175</td>\n",
       "      <td>57</td>\n",
       "      <td>87.7</td>\n",
       "      <td>A-</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1303</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_3</td>\n",
       "      <td>M</td>\n",
       "      <td>street_7</td>\n",
       "      <td>188</td>\n",
       "      <td>82</td>\n",
       "      <td>49.7</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1304</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_3</td>\n",
       "      <td>M</td>\n",
       "      <td>street_2</td>\n",
       "      <td>195</td>\n",
       "      <td>70</td>\n",
       "      <td>85.2</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1305</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_3</td>\n",
       "      <td>F</td>\n",
       "      <td>street_5</td>\n",
       "      <td>187</td>\n",
       "      <td>69</td>\n",
       "      <td>61.7</td>\n",
       "      <td>B-</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1102    S_1   C_1      F  street_2     192      73  32.5      B+\n",
       "1103    S_1   C_1      M  street_2     186      82  87.2      B+\n",
       "1104    S_1   C_1      F  street_2     167      81  80.4      B-\n",
       "1105    S_1   C_1      F  street_4     159      64  84.8      B+\n",
       "1201    S_1   C_2      M  street_5     188      68  97.0      A-\n",
       "1202    S_1   C_2      F  street_4     176      94  63.5      B-\n",
       "1203    S_1   C_2      M  street_6     160      53  58.8      A+\n",
       "1204    S_1   C_2      F  street_5     162      63  33.8       B\n",
       "1205    S_1   C_2      F  street_6     167      63  68.4      B-\n",
       "1301    S_1   C_3      M  street_4     161      68  31.5      B+\n",
       "1302    S_1   C_3      F  street_1     175      57  87.7      A-\n",
       "1303    S_1   C_3      M  street_7     188      82  49.7       B\n",
       "1304    S_1   C_3      M  street_2     195      70  85.2       A\n",
       "1305    S_1   C_3      F  street_5     187      69  61.7      B-"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#分组后必须要使用get_group函数才能取出某一组的值\n",
    "df.groupby('School').get_group('S_1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>School</th>\n",
       "      <th>Class</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Address</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Math</th>\n",
       "      <th>Physics</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2401</th>\n",
       "      <td>S_2</td>\n",
       "      <td>C_4</td>\n",
       "      <td>F</td>\n",
       "      <td>street_2</td>\n",
       "      <td>192</td>\n",
       "      <td>62</td>\n",
       "      <td>45.3</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2402</th>\n",
       "      <td>S_2</td>\n",
       "      <td>C_4</td>\n",
       "      <td>M</td>\n",
       "      <td>street_7</td>\n",
       "      <td>166</td>\n",
       "      <td>82</td>\n",
       "      <td>48.7</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2403</th>\n",
       "      <td>S_2</td>\n",
       "      <td>C_4</td>\n",
       "      <td>F</td>\n",
       "      <td>street_6</td>\n",
       "      <td>158</td>\n",
       "      <td>60</td>\n",
       "      <td>59.7</td>\n",
       "      <td>B+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2404</th>\n",
       "      <td>S_2</td>\n",
       "      <td>C_4</td>\n",
       "      <td>F</td>\n",
       "      <td>street_2</td>\n",
       "      <td>160</td>\n",
       "      <td>84</td>\n",
       "      <td>67.7</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2405</th>\n",
       "      <td>S_2</td>\n",
       "      <td>C_4</td>\n",
       "      <td>F</td>\n",
       "      <td>street_6</td>\n",
       "      <td>193</td>\n",
       "      <td>54</td>\n",
       "      <td>47.6</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "2401    S_2   C_4      F  street_2     192      62  45.3       A\n",
       "2402    S_2   C_4      M  street_7     166      82  48.7       B\n",
       "2403    S_2   C_4      F  street_6     158      60  59.7      B+\n",
       "2404    S_2   C_4      F  street_2     160      84  67.7       B\n",
       "2405    S_2   C_4      F  street_6     193      54  47.6       B"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#可以根据多列取出组\n",
    "df.groupby(['School','Class']).get_group(('S_2','C_4'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "School\n",
       "S_1    15\n",
       "S_2    20\n",
       "dtype: int64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算每一组的容量\n",
    "df.groupby('School').size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算每一组有几个样本\n",
    "df.groupby('School').ngroups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "S_1\n",
      "S_2\n"
     ]
    }
   ],
   "source": [
    "#进行组的遍历的时候要使用display函数\n",
    "for name,group in df.groupby('School'):\n",
    "    print(name)\n",
    "    group.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Address</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Math</th>\n",
       "      <th>Physics</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Gender</th>\n",
       "      <th>School</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"16\" valign=\"top\">M</th>\n",
       "      <th>S_1</th>\n",
       "      <td>C_1</td>\n",
       "      <td>street_1</td>\n",
       "      <td>173</td>\n",
       "      <td>63</td>\n",
       "      <td>34.0</td>\n",
       "      <td>A+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_1</th>\n",
       "      <td>C_1</td>\n",
       "      <td>street_2</td>\n",
       "      <td>186</td>\n",
       "      <td>82</td>\n",
       "      <td>87.2</td>\n",
       "      <td>B+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_1</th>\n",
       "      <td>C_2</td>\n",
       "      <td>street_5</td>\n",
       "      <td>188</td>\n",
       "      <td>68</td>\n",
       "      <td>97.0</td>\n",
       "      <td>A-</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_1</th>\n",
       "      <td>C_2</td>\n",
       "      <td>street_6</td>\n",
       "      <td>160</td>\n",
       "      <td>53</td>\n",
       "      <td>58.8</td>\n",
       "      <td>A+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_1</th>\n",
       "      <td>C_3</td>\n",
       "      <td>street_4</td>\n",
       "      <td>161</td>\n",
       "      <td>68</td>\n",
       "      <td>31.5</td>\n",
       "      <td>B+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_1</th>\n",
       "      <td>C_3</td>\n",
       "      <td>street_7</td>\n",
       "      <td>188</td>\n",
       "      <td>82</td>\n",
       "      <td>49.7</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_1</th>\n",
       "      <td>C_3</td>\n",
       "      <td>street_2</td>\n",
       "      <td>195</td>\n",
       "      <td>70</td>\n",
       "      <td>85.2</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_2</th>\n",
       "      <td>C_1</td>\n",
       "      <td>street_7</td>\n",
       "      <td>174</td>\n",
       "      <td>84</td>\n",
       "      <td>83.3</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_2</th>\n",
       "      <td>C_1</td>\n",
       "      <td>street_4</td>\n",
       "      <td>157</td>\n",
       "      <td>61</td>\n",
       "      <td>52.5</td>\n",
       "      <td>B-</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_2</th>\n",
       "      <td>C_1</td>\n",
       "      <td>street_4</td>\n",
       "      <td>170</td>\n",
       "      <td>81</td>\n",
       "      <td>34.2</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_2</th>\n",
       "      <td>C_2</td>\n",
       "      <td>street_5</td>\n",
       "      <td>193</td>\n",
       "      <td>100</td>\n",
       "      <td>39.1</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_2</th>\n",
       "      <td>C_2</td>\n",
       "      <td>street_4</td>\n",
       "      <td>155</td>\n",
       "      <td>91</td>\n",
       "      <td>73.8</td>\n",
       "      <td>A+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_2</th>\n",
       "      <td>C_2</td>\n",
       "      <td>street_1</td>\n",
       "      <td>175</td>\n",
       "      <td>74</td>\n",
       "      <td>47.2</td>\n",
       "      <td>B-</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_2</th>\n",
       "      <td>C_3</td>\n",
       "      <td>street_5</td>\n",
       "      <td>171</td>\n",
       "      <td>88</td>\n",
       "      <td>32.7</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_2</th>\n",
       "      <td>C_3</td>\n",
       "      <td>street_4</td>\n",
       "      <td>187</td>\n",
       "      <td>73</td>\n",
       "      <td>48.9</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_2</th>\n",
       "      <td>C_4</td>\n",
       "      <td>street_7</td>\n",
       "      <td>166</td>\n",
       "      <td>82</td>\n",
       "      <td>48.7</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Class   Address  Height  Weight  Math Physics\n",
       "Gender School                                              \n",
       "M      S_1      C_1  street_1     173      63  34.0      A+\n",
       "       S_1      C_1  street_2     186      82  87.2      B+\n",
       "       S_1      C_2  street_5     188      68  97.0      A-\n",
       "       S_1      C_2  street_6     160      53  58.8      A+\n",
       "       S_1      C_3  street_4     161      68  31.5      B+\n",
       "       S_1      C_3  street_7     188      82  49.7       B\n",
       "       S_1      C_3  street_2     195      70  85.2       A\n",
       "       S_2      C_1  street_7     174      84  83.3       C\n",
       "       S_2      C_1  street_4     157      61  52.5      B-\n",
       "       S_2      C_1  street_4     170      81  34.2       A\n",
       "       S_2      C_2  street_5     193     100  39.1       B\n",
       "       S_2      C_2  street_4     155      91  73.8      A+\n",
       "       S_2      C_2  street_1     175      74  47.2      B-\n",
       "       S_2      C_3  street_5     171      88  32.7       A\n",
       "       S_2      C_3  street_4     187      73  48.9       B\n",
       "       S_2      C_4  street_7     166      82  48.7       B"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#多级索引进行分组时 level用于选择第几个索引列\n",
    "df.set_index(['Gender','School']).groupby(level=0,axis=0).get_group('M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "grouped_single = df.groupby('School')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对分组对象使用head函数，返回的是每个组的前几行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>School</th>\n",
       "      <th>Class</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Address</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Math</th>\n",
       "      <th>Physics</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1101</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_1</td>\n",
       "      <td>M</td>\n",
       "      <td>street_1</td>\n",
       "      <td>173</td>\n",
       "      <td>63</td>\n",
       "      <td>34.0</td>\n",
       "      <td>A+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1102</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_1</td>\n",
       "      <td>F</td>\n",
       "      <td>street_2</td>\n",
       "      <td>192</td>\n",
       "      <td>73</td>\n",
       "      <td>32.5</td>\n",
       "      <td>B+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2101</th>\n",
       "      <td>S_2</td>\n",
       "      <td>C_1</td>\n",
       "      <td>M</td>\n",
       "      <td>street_7</td>\n",
       "      <td>174</td>\n",
       "      <td>84</td>\n",
       "      <td>83.3</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2102</th>\n",
       "      <td>S_2</td>\n",
       "      <td>C_1</td>\n",
       "      <td>F</td>\n",
       "      <td>street_6</td>\n",
       "      <td>161</td>\n",
       "      <td>61</td>\n",
       "      <td>50.6</td>\n",
       "      <td>B+</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1101    S_1   C_1      M  street_1     173      63  34.0      A+\n",
       "1102    S_1   C_1      F  street_2     192      73  32.5      B+\n",
       "2101    S_2   C_1      M  street_7     174      84  83.3       C\n",
       "2102    S_2   C_1      F  street_6     161      61  50.6      B+"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_single.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Address</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Math</th>\n",
       "      <th>Physics</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>School</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>S_1</th>\n",
       "      <td>C_1</td>\n",
       "      <td>M</td>\n",
       "      <td>street_1</td>\n",
       "      <td>173</td>\n",
       "      <td>63</td>\n",
       "      <td>34.0</td>\n",
       "      <td>A+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_2</th>\n",
       "      <td>C_1</td>\n",
       "      <td>M</td>\n",
       "      <td>street_7</td>\n",
       "      <td>174</td>\n",
       "      <td>84</td>\n",
       "      <td>83.3</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Class Gender   Address  Height  Weight  Math Physics\n",
       "School                                                     \n",
       "S_1      C_1      M  street_1     173      63  34.0      A+\n",
       "S_2      C_1      M  street_7     174      84  83.3       C"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#first 以分组为索引，每一个分组的第一个信息\n",
    "grouped_single.first()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>School</th>\n",
       "      <th>Class</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Address</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Math</th>\n",
       "      <th>Physics</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1102</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_1</td>\n",
       "      <td>F</td>\n",
       "      <td>street_2</td>\n",
       "      <td>192</td>\n",
       "      <td>73</td>\n",
       "      <td>32.5</td>\n",
       "      <td>B+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1104</th>\n",
       "      <td>S_1</td>\n",
       "      <td>C_1</td>\n",
       "      <td>F</td>\n",
       "      <td>street_2</td>\n",
       "      <td>167</td>\n",
       "      <td>81</td>\n",
       "      <td>80.4</td>\n",
       "      <td>B-</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2101</th>\n",
       "      <td>S_2</td>\n",
       "      <td>C_1</td>\n",
       "      <td>M</td>\n",
       "      <td>street_7</td>\n",
       "      <td>174</td>\n",
       "      <td>84</td>\n",
       "      <td>83.3</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2102</th>\n",
       "      <td>S_2</td>\n",
       "      <td>C_1</td>\n",
       "      <td>F</td>\n",
       "      <td>street_6</td>\n",
       "      <td>161</td>\n",
       "      <td>61</td>\n",
       "      <td>50.6</td>\n",
       "      <td>B+</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2103</th>\n",
       "      <td>S_2</td>\n",
       "      <td>C_1</td>\n",
       "      <td>M</td>\n",
       "      <td>street_4</td>\n",
       "      <td>157</td>\n",
       "      <td>61</td>\n",
       "      <td>52.5</td>\n",
       "      <td>B-</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2201</th>\n",
       "      <td>S_2</td>\n",
       "      <td>C_2</td>\n",
       "      <td>M</td>\n",
       "      <td>street_5</td>\n",
       "      <td>193</td>\n",
       "      <td>100</td>\n",
       "      <td>39.1</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2302</th>\n",
       "      <td>S_2</td>\n",
       "      <td>C_3</td>\n",
       "      <td>M</td>\n",
       "      <td>street_5</td>\n",
       "      <td>171</td>\n",
       "      <td>88</td>\n",
       "      <td>32.7</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2404</th>\n",
       "      <td>S_2</td>\n",
       "      <td>C_4</td>\n",
       "      <td>F</td>\n",
       "      <td>street_2</td>\n",
       "      <td>160</td>\n",
       "      <td>84</td>\n",
       "      <td>67.7</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     School Class Gender   Address  Height  Weight  Math Physics\n",
       "ID                                                              \n",
       "1102    S_1   C_1      F  street_2     192      73  32.5      B+\n",
       "1104    S_1   C_1      F  street_2     167      81  80.4      B-\n",
       "2101    S_2   C_1      M  street_7     174      84  83.3       C\n",
       "2102    S_2   C_1      F  street_6     161      61  50.6      B+\n",
       "2103    S_2   C_1      M  street_4     157      61  52.5      B-\n",
       "2201    S_2   C_2      M  street_5     193     100  39.1       B\n",
       "2302    S_2   C_3      M  street_5     171      88  32.7       A\n",
       "2404    S_2   C_4      F  street_2     160      84  67.7       B"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(np.random.choice(['a','b','c'],df.shape[0])).get_group('a')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当使用groupby函数时，传入的对象就是索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pandas.core.groupby.generic.SeriesGroupBy object at 0x000001EBEE8D4EB8>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#并不会显示任何内容\n",
    "df.groupby(['Gender','School'])['Math']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Gender  School\n",
       "F       S_1       64.100000\n",
       "        S_2       66.427273\n",
       "M       S_1       63.342857\n",
       "        S_2       51.155556\n",
       "Name: Math, dtype: float64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#取到每一个组的均值\n",
    "df.groupby(['Gender','School'])['Math'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Math</th>\n",
       "      <th>Height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Gender</th>\n",
       "      <th>School</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">F</th>\n",
       "      <th>S_1</th>\n",
       "      <td>64.100000</td>\n",
       "      <td>173.125000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_2</th>\n",
       "      <td>66.427273</td>\n",
       "      <td>173.727273</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">M</th>\n",
       "      <th>S_1</th>\n",
       "      <td>63.342857</td>\n",
       "      <td>178.714286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_2</th>\n",
       "      <td>51.155556</td>\n",
       "      <td>172.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    Math      Height\n",
       "Gender School                       \n",
       "F      S_1     64.100000  173.125000\n",
       "       S_2     66.427273  173.727273\n",
       "M      S_1     63.342857  178.714286\n",
       "       S_2     51.155556  172.000000"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#可以选出多个属性列 对每一个列进行单独的聚合操作\n",
    "df.groupby(['Gender','School'])[['Math','Height']].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Math\n",
       "(0, 40]       7\n",
       "(40, 60]     10\n",
       "(60, 80]      9\n",
       "(80, 90]      7\n",
       "(90, 100]     2\n",
       "Name: Math, dtype: int64"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#连续性变量分组前需使用cut函数离散化操作\n",
    "bins = [0,40,60,80,90,100]\n",
    "cuts = pd.cut(df['Math'],bins=bins)\n",
    "df.groupby(cuts)['Math'].count()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "所谓聚合就是把一堆数，变成一个标量，因此mean/sum/size/count/std/var/sem/describe/first/last/nth/min/max都是聚合函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "group_m = grouped_single['Math']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sum</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>School</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>S_1</th>\n",
       "      <td>956.2</td>\n",
       "      <td>63.746667</td>\n",
       "      <td>23.077474</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_2</th>\n",
       "      <td>1191.1</td>\n",
       "      <td>59.555000</td>\n",
       "      <td>17.589305</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           sum       mean        std\n",
       "School                              \n",
       "S_1      956.2  63.746667  23.077474\n",
       "S_2     1191.1  59.555000  17.589305"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#同时使用多个聚合函数 要使用agg函数\n",
    "group_m.agg(['sum','mean','std'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>rename_sum</th>\n",
       "      <th>rename_mean</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>School</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>S_1</th>\n",
       "      <td>956.2</td>\n",
       "      <td>63.746667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_2</th>\n",
       "      <td>1191.1</td>\n",
       "      <td>59.555000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        rename_sum  rename_mean\n",
       "School                         \n",
       "S_1          956.2    63.746667\n",
       "S_2         1191.1    59.555000"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#利用元组进行重命名\n",
    "group_m.agg([('rename_sum','sum'),('rename_mean','mean')])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">Math</th>\n",
       "      <th>Height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>max</th>\n",
       "      <th>var</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>School</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>S_1</th>\n",
       "      <td>63.746667</td>\n",
       "      <td>97.0</td>\n",
       "      <td>161.638095</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_2</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>95.5</td>\n",
       "      <td>205.523684</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             Math            Height\n",
       "             mean   max         var\n",
       "School                             \n",
       "S_1     63.746667  97.0  161.638095\n",
       "S_2     59.555000  95.5  205.523684"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#可以使用字典指定哪些函数作用于哪些列\n",
    "grouped_single.agg({'Math':['mean','max'],'Height':'var'})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "agg函数的传入是分组（一组作为一列进行传入的）\n",
    "agg函数的本质是对多个聚合函数进行操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "School\n",
       "S_1    65.5\n",
       "S_2    62.8\n",
       "Name: Math, dtype: float64"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_single['Math'].agg(lambda x:x.max()-x.min())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "判断是否组内数学分数至少有一个值在50-52之间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "School\n",
       "S_1    False\n",
       "S_2     True\n",
       "Name: Math, dtype: bool"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#带参数的聚合函数\n",
    "def f(s,low,high):\n",
    "    return s.between(low,high).max()\n",
    "grouped_single['Math'].agg(f,50,52)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "filter函数是用来筛选某些组的（务必记住结果是组的全体）,如果该组中有一个值不满足条件则该组就会被pass，因此传入的值应当是布尔标量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>School</th>\n",
       "      <th>Math</th>\n",
       "      <th>Physics</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [School, Math, Physics]\n",
       "Index: []"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_single[['School','Math','Physics']].filter(lambda x:(x['Math']>50).all())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Math</th>\n",
       "      <th>Height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1101</th>\n",
       "      <td>2.5</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1102</th>\n",
       "      <td>1.0</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1103</th>\n",
       "      <td>55.7</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1104</th>\n",
       "      <td>48.9</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1105</th>\n",
       "      <td>53.3</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1201</th>\n",
       "      <td>65.5</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1202</th>\n",
       "      <td>32.0</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1203</th>\n",
       "      <td>27.3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1204</th>\n",
       "      <td>2.3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1205</th>\n",
       "      <td>36.9</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1301</th>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1302</th>\n",
       "      <td>56.2</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1303</th>\n",
       "      <td>18.2</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1304</th>\n",
       "      <td>53.7</td>\n",
       "      <td>36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1305</th>\n",
       "      <td>30.2</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2101</th>\n",
       "      <td>50.6</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2102</th>\n",
       "      <td>17.9</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2103</th>\n",
       "      <td>19.8</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2104</th>\n",
       "      <td>39.5</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2105</th>\n",
       "      <td>1.5</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2201</th>\n",
       "      <td>6.4</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2202</th>\n",
       "      <td>35.8</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2203</th>\n",
       "      <td>41.1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2204</th>\n",
       "      <td>14.5</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2205</th>\n",
       "      <td>52.7</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2301</th>\n",
       "      <td>39.6</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2302</th>\n",
       "      <td>0.0</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2303</th>\n",
       "      <td>33.2</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2304</th>\n",
       "      <td>62.8</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2305</th>\n",
       "      <td>16.2</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2401</th>\n",
       "      <td>12.6</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2402</th>\n",
       "      <td>16.0</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2403</th>\n",
       "      <td>27.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2404</th>\n",
       "      <td>35.0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2405</th>\n",
       "      <td>14.9</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Math  Height\n",
       "ID                \n",
       "1101   2.5      14\n",
       "1102   1.0      33\n",
       "1103  55.7      27\n",
       "1104  48.9       8\n",
       "1105  53.3       0\n",
       "1201  65.5      29\n",
       "1202  32.0      17\n",
       "1203  27.3       1\n",
       "1204   2.3       3\n",
       "1205  36.9       8\n",
       "1301   0.0       2\n",
       "1302  56.2      16\n",
       "1303  18.2      29\n",
       "1304  53.7      36\n",
       "1305  30.2      28\n",
       "2101  50.6      19\n",
       "2102  17.9       6\n",
       "2103  19.8       2\n",
       "2104  39.5       4\n",
       "2105   1.5      15\n",
       "2201   6.4      38\n",
       "2202  35.8      39\n",
       "2203  41.1       0\n",
       "2204  14.5      20\n",
       "2205  52.7      28\n",
       "2301  39.6       2\n",
       "2302   0.0      16\n",
       "2303  33.2      35\n",
       "2304  62.8       9\n",
       "2305  16.2      32\n",
       "2401  12.6      37\n",
       "2402  16.0      11\n",
       "2403  27.0       3\n",
       "2404  35.0       5\n",
       "2405  14.9      38"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#transform函数输入的是组内的列 transform函数一般需要搭配lambda函数\n",
    "grouped_single[['Math','Height']].transform(lambda x:x-x.min())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Math</th>\n",
       "      <th>Height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1101</th>\n",
       "      <td>63.746667</td>\n",
       "      <td>175.733333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1102</th>\n",
       "      <td>63.746667</td>\n",
       "      <td>175.733333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1103</th>\n",
       "      <td>63.746667</td>\n",
       "      <td>175.733333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1104</th>\n",
       "      <td>63.746667</td>\n",
       "      <td>175.733333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1105</th>\n",
       "      <td>63.746667</td>\n",
       "      <td>175.733333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1201</th>\n",
       "      <td>63.746667</td>\n",
       "      <td>175.733333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1202</th>\n",
       "      <td>63.746667</td>\n",
       "      <td>175.733333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1203</th>\n",
       "      <td>63.746667</td>\n",
       "      <td>175.733333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1204</th>\n",
       "      <td>63.746667</td>\n",
       "      <td>175.733333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1205</th>\n",
       "      <td>63.746667</td>\n",
       "      <td>175.733333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1301</th>\n",
       "      <td>63.746667</td>\n",
       "      <td>175.733333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1302</th>\n",
       "      <td>63.746667</td>\n",
       "      <td>175.733333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1303</th>\n",
       "      <td>63.746667</td>\n",
       "      <td>175.733333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1304</th>\n",
       "      <td>63.746667</td>\n",
       "      <td>175.733333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1305</th>\n",
       "      <td>63.746667</td>\n",
       "      <td>175.733333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2101</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2102</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2103</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2104</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2105</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2201</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2202</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2203</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2204</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2205</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2301</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2302</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2303</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2304</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2305</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2401</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2402</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2403</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2404</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2405</th>\n",
       "      <td>59.555000</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           Math      Height\n",
       "ID                         \n",
       "1101  63.746667  175.733333\n",
       "1102  63.746667  175.733333\n",
       "1103  63.746667  175.733333\n",
       "1104  63.746667  175.733333\n",
       "1105  63.746667  175.733333\n",
       "1201  63.746667  175.733333\n",
       "1202  63.746667  175.733333\n",
       "1203  63.746667  175.733333\n",
       "1204  63.746667  175.733333\n",
       "1205  63.746667  175.733333\n",
       "1301  63.746667  175.733333\n",
       "1302  63.746667  175.733333\n",
       "1303  63.746667  175.733333\n",
       "1304  63.746667  175.733333\n",
       "1305  63.746667  175.733333\n",
       "2101  59.555000  172.950000\n",
       "2102  59.555000  172.950000\n",
       "2103  59.555000  172.950000\n",
       "2104  59.555000  172.950000\n",
       "2105  59.555000  172.950000\n",
       "2201  59.555000  172.950000\n",
       "2202  59.555000  172.950000\n",
       "2203  59.555000  172.950000\n",
       "2204  59.555000  172.950000\n",
       "2205  59.555000  172.950000\n",
       "2301  59.555000  172.950000\n",
       "2302  59.555000  172.950000\n",
       "2303  59.555000  172.950000\n",
       "2304  59.555000  172.950000\n",
       "2305  59.555000  172.950000\n",
       "2401  59.555000  172.950000\n",
       "2402  59.555000  172.950000\n",
       "2403  59.555000  172.950000\n",
       "2404  59.555000  172.950000\n",
       "2405  59.555000  172.950000"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#transform函数中如果传入的是标量，那么组内的所有元素都会被广播为这个值\n",
    "grouped_single[['Math','Height']].transform(lambda x:x.mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Math</th>\n",
       "      <th>Height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1101</th>\n",
       "      <td>-1.288991</td>\n",
       "      <td>-0.214991</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1102</th>\n",
       "      <td>-1.353990</td>\n",
       "      <td>1.279460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1103</th>\n",
       "      <td>1.016287</td>\n",
       "      <td>0.807528</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1104</th>\n",
       "      <td>0.721627</td>\n",
       "      <td>-0.686923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1105</th>\n",
       "      <td>0.912289</td>\n",
       "      <td>-1.316166</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1201</th>\n",
       "      <td>1.440943</td>\n",
       "      <td>0.964839</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1202</th>\n",
       "      <td>-0.010689</td>\n",
       "      <td>0.020975</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1203</th>\n",
       "      <td>-0.214350</td>\n",
       "      <td>-1.237510</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1204</th>\n",
       "      <td>-1.297658</td>\n",
       "      <td>-1.080200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1205</th>\n",
       "      <td>0.201640</td>\n",
       "      <td>-0.686923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1301</th>\n",
       "      <td>-1.397322</td>\n",
       "      <td>-1.158855</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1302</th>\n",
       "      <td>1.037953</td>\n",
       "      <td>-0.057681</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1303</th>\n",
       "      <td>-0.608674</td>\n",
       "      <td>0.964839</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1304</th>\n",
       "      <td>0.929622</td>\n",
       "      <td>1.515426</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1305</th>\n",
       "      <td>-0.088687</td>\n",
       "      <td>0.886183</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2101</th>\n",
       "      <td>1.349968</td>\n",
       "      <td>0.073242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2102</th>\n",
       "      <td>-0.509116</td>\n",
       "      <td>-0.833560</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2103</th>\n",
       "      <td>-0.401096</td>\n",
       "      <td>-1.112576</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2104</th>\n",
       "      <td>0.718903</td>\n",
       "      <td>-0.973068</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2105</th>\n",
       "      <td>-1.441501</td>\n",
       "      <td>-0.205774</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2201</th>\n",
       "      <td>-1.162923</td>\n",
       "      <td>1.398568</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2202</th>\n",
       "      <td>0.508548</td>\n",
       "      <td>1.468322</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2203</th>\n",
       "      <td>0.809867</td>\n",
       "      <td>-1.252084</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2204</th>\n",
       "      <td>-0.702415</td>\n",
       "      <td>0.142996</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2205</th>\n",
       "      <td>1.469359</td>\n",
       "      <td>0.701028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2301</th>\n",
       "      <td>0.724588</td>\n",
       "      <td>-1.112576</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2302</th>\n",
       "      <td>-1.526780</td>\n",
       "      <td>-0.136020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2303</th>\n",
       "      <td>0.360731</td>\n",
       "      <td>1.189306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2304</th>\n",
       "      <td>2.043571</td>\n",
       "      <td>-0.624298</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2305</th>\n",
       "      <td>-0.605766</td>\n",
       "      <td>0.980044</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2401</th>\n",
       "      <td>-0.810436</td>\n",
       "      <td>1.328814</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2402</th>\n",
       "      <td>-0.617136</td>\n",
       "      <td>-0.484790</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2403</th>\n",
       "      <td>0.008244</td>\n",
       "      <td>-1.042822</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2404</th>\n",
       "      <td>0.463065</td>\n",
       "      <td>-0.903314</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2405</th>\n",
       "      <td>-0.679674</td>\n",
       "      <td>1.398568</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Math    Height\n",
       "ID                      \n",
       "1101 -1.288991 -0.214991\n",
       "1102 -1.353990  1.279460\n",
       "1103  1.016287  0.807528\n",
       "1104  0.721627 -0.686923\n",
       "1105  0.912289 -1.316166\n",
       "1201  1.440943  0.964839\n",
       "1202 -0.010689  0.020975\n",
       "1203 -0.214350 -1.237510\n",
       "1204 -1.297658 -1.080200\n",
       "1205  0.201640 -0.686923\n",
       "1301 -1.397322 -1.158855\n",
       "1302  1.037953 -0.057681\n",
       "1303 -0.608674  0.964839\n",
       "1304  0.929622  1.515426\n",
       "1305 -0.088687  0.886183\n",
       "2101  1.349968  0.073242\n",
       "2102 -0.509116 -0.833560\n",
       "2103 -0.401096 -1.112576\n",
       "2104  0.718903 -0.973068\n",
       "2105 -1.441501 -0.205774\n",
       "2201 -1.162923  1.398568\n",
       "2202  0.508548  1.468322\n",
       "2203  0.809867 -1.252084\n",
       "2204 -0.702415  0.142996\n",
       "2205  1.469359  0.701028\n",
       "2301  0.724588 -1.112576\n",
       "2302 -1.526780 -0.136020\n",
       "2303  0.360731  1.189306\n",
       "2304  2.043571 -0.624298\n",
       "2305 -0.605766  0.980044\n",
       "2401 -0.810436  1.328814\n",
       "2402 -0.617136 -0.484790\n",
       "2403  0.008244 -1.042822\n",
       "2404  0.463065 -0.903314\n",
       "2405 -0.679674  1.398568"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#利用变换方法进行组内标准化\n",
    "grouped_single[['Math','Height']].transform(lambda x:(x-x.mean())/x.std())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "重要：利用变换方法进行组内缺失值的均值填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.groupby('School').transform(lambda x:x.fillna(x.mean()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "apply是以分组的表传入到函数中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>School</th>\n",
       "      <th>Math</th>\n",
       "      <th>Height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>School</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>S_1</th>\n",
       "      <td>S_1</td>\n",
       "      <td>97.0</td>\n",
       "      <td>195</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_2</th>\n",
       "      <td>S_2</td>\n",
       "      <td>95.5</td>\n",
       "      <td>194</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       School  Math  Height\n",
       "School                     \n",
       "S_1       S_1  97.0     195\n",
       "S_2       S_2  95.5     194"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['School','Math','Height']].groupby('School').apply(lambda x:x.max())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Math</th>\n",
       "      <th>Height</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1101</th>\n",
       "      <td>2.5</td>\n",
       "      <td>14.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1102</th>\n",
       "      <td>1.0</td>\n",
       "      <td>33.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1103</th>\n",
       "      <td>55.7</td>\n",
       "      <td>27.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1104</th>\n",
       "      <td>48.9</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1105</th>\n",
       "      <td>53.3</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Math  Height\n",
       "ID                \n",
       "1101   2.5    14.0\n",
       "1102   1.0    33.0\n",
       "1103  55.7    27.0\n",
       "1104  48.9     8.0\n",
       "1105  53.3     0.0"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['School','Math','Height']].groupby('School').apply(lambda x:x-x.min()).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>col1</th>\n",
       "      <th>col2</th>\n",
       "      <th>col3</th>\n",
       "      <th>col4</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1101</th>\n",
       "      <td>-63.0</td>\n",
       "      <td>2.5</td>\n",
       "      <td>-22</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1102</th>\n",
       "      <td>-64.5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-3</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1103</th>\n",
       "      <td>-9.8</td>\n",
       "      <td>55.7</td>\n",
       "      <td>-9</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1104</th>\n",
       "      <td>-16.6</td>\n",
       "      <td>48.9</td>\n",
       "      <td>-28</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1105</th>\n",
       "      <td>-12.2</td>\n",
       "      <td>53.3</td>\n",
       "      <td>-36</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1201</th>\n",
       "      <td>0.0</td>\n",
       "      <td>65.5</td>\n",
       "      <td>-7</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1202</th>\n",
       "      <td>-33.5</td>\n",
       "      <td>32.0</td>\n",
       "      <td>-19</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1203</th>\n",
       "      <td>-38.2</td>\n",
       "      <td>27.3</td>\n",
       "      <td>-35</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1204</th>\n",
       "      <td>-63.2</td>\n",
       "      <td>2.3</td>\n",
       "      <td>-33</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1205</th>\n",
       "      <td>-28.6</td>\n",
       "      <td>36.9</td>\n",
       "      <td>-28</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1301</th>\n",
       "      <td>-65.5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-34</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1302</th>\n",
       "      <td>-9.3</td>\n",
       "      <td>56.2</td>\n",
       "      <td>-20</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1303</th>\n",
       "      <td>-47.3</td>\n",
       "      <td>18.2</td>\n",
       "      <td>-7</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1304</th>\n",
       "      <td>-11.8</td>\n",
       "      <td>53.7</td>\n",
       "      <td>0</td>\n",
       "      <td>36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1305</th>\n",
       "      <td>-35.3</td>\n",
       "      <td>30.2</td>\n",
       "      <td>-8</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2101</th>\n",
       "      <td>-12.2</td>\n",
       "      <td>50.6</td>\n",
       "      <td>-20</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2102</th>\n",
       "      <td>-44.9</td>\n",
       "      <td>17.9</td>\n",
       "      <td>-33</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2103</th>\n",
       "      <td>-43.0</td>\n",
       "      <td>19.8</td>\n",
       "      <td>-37</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2104</th>\n",
       "      <td>-23.3</td>\n",
       "      <td>39.5</td>\n",
       "      <td>-35</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2105</th>\n",
       "      <td>-61.3</td>\n",
       "      <td>1.5</td>\n",
       "      <td>-24</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2201</th>\n",
       "      <td>-56.4</td>\n",
       "      <td>6.4</td>\n",
       "      <td>-1</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2202</th>\n",
       "      <td>-27.0</td>\n",
       "      <td>35.8</td>\n",
       "      <td>0</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2203</th>\n",
       "      <td>-21.7</td>\n",
       "      <td>41.1</td>\n",
       "      <td>-39</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2204</th>\n",
       "      <td>-48.3</td>\n",
       "      <td>14.5</td>\n",
       "      <td>-19</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2205</th>\n",
       "      <td>-10.1</td>\n",
       "      <td>52.7</td>\n",
       "      <td>-11</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2301</th>\n",
       "      <td>-23.2</td>\n",
       "      <td>39.6</td>\n",
       "      <td>-37</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2302</th>\n",
       "      <td>-62.8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-23</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2303</th>\n",
       "      <td>-29.6</td>\n",
       "      <td>33.2</td>\n",
       "      <td>-4</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2304</th>\n",
       "      <td>0.0</td>\n",
       "      <td>62.8</td>\n",
       "      <td>-30</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2305</th>\n",
       "      <td>-46.6</td>\n",
       "      <td>16.2</td>\n",
       "      <td>-7</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2401</th>\n",
       "      <td>-50.2</td>\n",
       "      <td>12.6</td>\n",
       "      <td>-2</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2402</th>\n",
       "      <td>-46.8</td>\n",
       "      <td>16.0</td>\n",
       "      <td>-28</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2403</th>\n",
       "      <td>-35.8</td>\n",
       "      <td>27.0</td>\n",
       "      <td>-36</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2404</th>\n",
       "      <td>-27.8</td>\n",
       "      <td>35.0</td>\n",
       "      <td>-34</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2405</th>\n",
       "      <td>-47.9</td>\n",
       "      <td>14.9</td>\n",
       "      <td>-1</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      col1  col2  col3  col4\n",
       "ID                          \n",
       "1101 -63.0   2.5   -22    14\n",
       "1102 -64.5   1.0    -3    33\n",
       "1103  -9.8  55.7    -9    27\n",
       "1104 -16.6  48.9   -28     8\n",
       "1105 -12.2  53.3   -36     0\n",
       "1201   0.0  65.5    -7    29\n",
       "1202 -33.5  32.0   -19    17\n",
       "1203 -38.2  27.3   -35     1\n",
       "1204 -63.2   2.3   -33     3\n",
       "1205 -28.6  36.9   -28     8\n",
       "1301 -65.5   0.0   -34     2\n",
       "1302  -9.3  56.2   -20    16\n",
       "1303 -47.3  18.2    -7    29\n",
       "1304 -11.8  53.7     0    36\n",
       "1305 -35.3  30.2    -8    28\n",
       "2101 -12.2  50.6   -20    19\n",
       "2102 -44.9  17.9   -33     6\n",
       "2103 -43.0  19.8   -37     2\n",
       "2104 -23.3  39.5   -35     4\n",
       "2105 -61.3   1.5   -24    15\n",
       "2201 -56.4   6.4    -1    38\n",
       "2202 -27.0  35.8     0    39\n",
       "2203 -21.7  41.1   -39     0\n",
       "2204 -48.3  14.5   -19    20\n",
       "2205 -10.1  52.7   -11    28\n",
       "2301 -23.2  39.6   -37     2\n",
       "2302 -62.8   0.0   -23    16\n",
       "2303 -29.6  33.2    -4    35\n",
       "2304   0.0  62.8   -30     9\n",
       "2305 -46.6  16.2    -7    32\n",
       "2401 -50.2  12.6    -2    37\n",
       "2402 -46.8  16.0   -28    11\n",
       "2403 -35.8  27.0   -36     3\n",
       "2404 -27.8  35.0   -34     5\n",
       "2405 -47.9  14.9    -1    38"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['School','Math','Height']].groupby('School')\\\n",
    "    .apply(lambda x:pd.DataFrame({'col1':x['Math']-x['Math'].max(),\n",
    "                                  'col2':x['Math']-x['Math'].min(),\n",
    "                                  'col3':x['Height']-x['Height'].max(),\n",
    "                                  'col4':x['Height']-x['Height'].min()}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>M_sum</th>\n",
       "      <th>W_var</th>\n",
       "      <th>H_mean</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>School</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>S_1</th>\n",
       "      <td>956.2</td>\n",
       "      <td>117.428571</td>\n",
       "      <td>175.733333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>S_2</th>\n",
       "      <td>1191.1</td>\n",
       "      <td>181.081579</td>\n",
       "      <td>172.950000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         M_sum       W_var      H_mean\n",
       "School                                \n",
       "S_1      956.2  117.428571  175.733333\n",
       "S_2     1191.1  181.081579  172.950000"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用apply函数同时统计多个指标  统计每一组的多个聚合函数\n",
    "from collections import OrderedDict\n",
    "def f(df):\n",
    "    data = OrderedDict()\n",
    "    data['M_sum'] = df['Math'].sum()\n",
    "    data['W_var'] = df['Weight'].var()\n",
    "    data['H_mean'] = df['Height'].mean()\n",
    "    return pd.Series(data)\n",
    "grouped_single.apply(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('data/Diamonds.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>carat</th>\n",
       "      <th>color</th>\n",
       "      <th>depth</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.23</td>\n",
       "      <td>E</td>\n",
       "      <td>61.5</td>\n",
       "      <td>326</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.21</td>\n",
       "      <td>E</td>\n",
       "      <td>59.8</td>\n",
       "      <td>326</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.23</td>\n",
       "      <td>E</td>\n",
       "      <td>56.9</td>\n",
       "      <td>327</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.29</td>\n",
       "      <td>I</td>\n",
       "      <td>62.4</td>\n",
       "      <td>334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.31</td>\n",
       "      <td>J</td>\n",
       "      <td>63.3</td>\n",
       "      <td>335</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.24</td>\n",
       "      <td>J</td>\n",
       "      <td>62.8</td>\n",
       "      <td>336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.24</td>\n",
       "      <td>I</td>\n",
       "      <td>62.3</td>\n",
       "      <td>336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.26</td>\n",
       "      <td>H</td>\n",
       "      <td>61.9</td>\n",
       "      <td>337</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.22</td>\n",
       "      <td>E</td>\n",
       "      <td>65.1</td>\n",
       "      <td>337</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0.23</td>\n",
       "      <td>H</td>\n",
       "      <td>59.4</td>\n",
       "      <td>338</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0.30</td>\n",
       "      <td>J</td>\n",
       "      <td>64.0</td>\n",
       "      <td>339</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>0.23</td>\n",
       "      <td>J</td>\n",
       "      <td>62.8</td>\n",
       "      <td>340</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>0.22</td>\n",
       "      <td>F</td>\n",
       "      <td>60.4</td>\n",
       "      <td>342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0.31</td>\n",
       "      <td>J</td>\n",
       "      <td>62.2</td>\n",
       "      <td>344</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>0.20</td>\n",
       "      <td>E</td>\n",
       "      <td>60.2</td>\n",
       "      <td>345</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0.32</td>\n",
       "      <td>E</td>\n",
       "      <td>60.9</td>\n",
       "      <td>345</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0.30</td>\n",
       "      <td>I</td>\n",
       "      <td>62.0</td>\n",
       "      <td>348</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>0.30</td>\n",
       "      <td>J</td>\n",
       "      <td>63.4</td>\n",
       "      <td>351</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>0.30</td>\n",
       "      <td>J</td>\n",
       "      <td>63.8</td>\n",
       "      <td>351</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>0.30</td>\n",
       "      <td>J</td>\n",
       "      <td>62.7</td>\n",
       "      <td>351</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>0.30</td>\n",
       "      <td>I</td>\n",
       "      <td>63.3</td>\n",
       "      <td>351</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>0.23</td>\n",
       "      <td>E</td>\n",
       "      <td>63.8</td>\n",
       "      <td>352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>0.23</td>\n",
       "      <td>H</td>\n",
       "      <td>61.0</td>\n",
       "      <td>353</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>0.31</td>\n",
       "      <td>J</td>\n",
       "      <td>59.4</td>\n",
       "      <td>353</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>0.31</td>\n",
       "      <td>J</td>\n",
       "      <td>58.1</td>\n",
       "      <td>353</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>0.23</td>\n",
       "      <td>G</td>\n",
       "      <td>60.4</td>\n",
       "      <td>354</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>0.24</td>\n",
       "      <td>I</td>\n",
       "      <td>62.5</td>\n",
       "      <td>355</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>0.30</td>\n",
       "      <td>J</td>\n",
       "      <td>62.2</td>\n",
       "      <td>357</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>0.23</td>\n",
       "      <td>D</td>\n",
       "      <td>60.5</td>\n",
       "      <td>357</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>0.23</td>\n",
       "      <td>F</td>\n",
       "      <td>60.9</td>\n",
       "      <td>357</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53910</th>\n",
       "      <td>0.70</td>\n",
       "      <td>E</td>\n",
       "      <td>60.5</td>\n",
       "      <td>2753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53911</th>\n",
       "      <td>0.57</td>\n",
       "      <td>E</td>\n",
       "      <td>59.8</td>\n",
       "      <td>2753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53912</th>\n",
       "      <td>0.61</td>\n",
       "      <td>F</td>\n",
       "      <td>61.8</td>\n",
       "      <td>2753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53913</th>\n",
       "      <td>0.80</td>\n",
       "      <td>G</td>\n",
       "      <td>64.2</td>\n",
       "      <td>2753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53914</th>\n",
       "      <td>0.84</td>\n",
       "      <td>I</td>\n",
       "      <td>63.7</td>\n",
       "      <td>2753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53915</th>\n",
       "      <td>0.77</td>\n",
       "      <td>E</td>\n",
       "      <td>62.1</td>\n",
       "      <td>2753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53916</th>\n",
       "      <td>0.74</td>\n",
       "      <td>D</td>\n",
       "      <td>63.1</td>\n",
       "      <td>2753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53917</th>\n",
       "      <td>0.90</td>\n",
       "      <td>J</td>\n",
       "      <td>63.2</td>\n",
       "      <td>2753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53918</th>\n",
       "      <td>0.76</td>\n",
       "      <td>I</td>\n",
       "      <td>59.3</td>\n",
       "      <td>2753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53919</th>\n",
       "      <td>0.76</td>\n",
       "      <td>I</td>\n",
       "      <td>62.2</td>\n",
       "      <td>2753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53920</th>\n",
       "      <td>0.70</td>\n",
       "      <td>E</td>\n",
       "      <td>62.4</td>\n",
       "      <td>2755</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53921</th>\n",
       "      <td>0.70</td>\n",
       "      <td>E</td>\n",
       "      <td>62.8</td>\n",
       "      <td>2755</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53922</th>\n",
       "      <td>0.70</td>\n",
       "      <td>D</td>\n",
       "      <td>63.1</td>\n",
       "      <td>2755</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53923</th>\n",
       "      <td>0.73</td>\n",
       "      <td>I</td>\n",
       "      <td>61.3</td>\n",
       "      <td>2756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53924</th>\n",
       "      <td>0.73</td>\n",
       "      <td>I</td>\n",
       "      <td>61.6</td>\n",
       "      <td>2756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53925</th>\n",
       "      <td>0.79</td>\n",
       "      <td>I</td>\n",
       "      <td>61.6</td>\n",
       "      <td>2756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53926</th>\n",
       "      <td>0.71</td>\n",
       "      <td>E</td>\n",
       "      <td>61.9</td>\n",
       "      <td>2756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53927</th>\n",
       "      <td>0.79</td>\n",
       "      <td>F</td>\n",
       "      <td>58.1</td>\n",
       "      <td>2756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53928</th>\n",
       "      <td>0.79</td>\n",
       "      <td>E</td>\n",
       "      <td>61.4</td>\n",
       "      <td>2756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53929</th>\n",
       "      <td>0.71</td>\n",
       "      <td>G</td>\n",
       "      <td>61.4</td>\n",
       "      <td>2756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53930</th>\n",
       "      <td>0.71</td>\n",
       "      <td>E</td>\n",
       "      <td>60.5</td>\n",
       "      <td>2756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53931</th>\n",
       "      <td>0.71</td>\n",
       "      <td>F</td>\n",
       "      <td>59.8</td>\n",
       "      <td>2756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53932</th>\n",
       "      <td>0.70</td>\n",
       "      <td>E</td>\n",
       "      <td>60.5</td>\n",
       "      <td>2757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53933</th>\n",
       "      <td>0.70</td>\n",
       "      <td>E</td>\n",
       "      <td>61.2</td>\n",
       "      <td>2757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53934</th>\n",
       "      <td>0.72</td>\n",
       "      <td>D</td>\n",
       "      <td>62.7</td>\n",
       "      <td>2757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53935</th>\n",
       "      <td>0.72</td>\n",
       "      <td>D</td>\n",
       "      <td>60.8</td>\n",
       "      <td>2757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53936</th>\n",
       "      <td>0.72</td>\n",
       "      <td>D</td>\n",
       "      <td>63.1</td>\n",
       "      <td>2757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53937</th>\n",
       "      <td>0.70</td>\n",
       "      <td>D</td>\n",
       "      <td>62.8</td>\n",
       "      <td>2757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53938</th>\n",
       "      <td>0.86</td>\n",
       "      <td>H</td>\n",
       "      <td>61.0</td>\n",
       "      <td>2757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53939</th>\n",
       "      <td>0.75</td>\n",
       "      <td>D</td>\n",
       "      <td>62.2</td>\n",
       "      <td>2757</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>53940 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       carat color  depth  price\n",
       "0       0.23     E   61.5    326\n",
       "1       0.21     E   59.8    326\n",
       "2       0.23     E   56.9    327\n",
       "3       0.29     I   62.4    334\n",
       "4       0.31     J   63.3    335\n",
       "5       0.24     J   62.8    336\n",
       "6       0.24     I   62.3    336\n",
       "7       0.26     H   61.9    337\n",
       "8       0.22     E   65.1    337\n",
       "9       0.23     H   59.4    338\n",
       "10      0.30     J   64.0    339\n",
       "11      0.23     J   62.8    340\n",
       "12      0.22     F   60.4    342\n",
       "13      0.31     J   62.2    344\n",
       "14      0.20     E   60.2    345\n",
       "15      0.32     E   60.9    345\n",
       "16      0.30     I   62.0    348\n",
       "17      0.30     J   63.4    351\n",
       "18      0.30     J   63.8    351\n",
       "19      0.30     J   62.7    351\n",
       "20      0.30     I   63.3    351\n",
       "21      0.23     E   63.8    352\n",
       "22      0.23     H   61.0    353\n",
       "23      0.31     J   59.4    353\n",
       "24      0.31     J   58.1    353\n",
       "25      0.23     G   60.4    354\n",
       "26      0.24     I   62.5    355\n",
       "27      0.30     J   62.2    357\n",
       "28      0.23     D   60.5    357\n",
       "29      0.23     F   60.9    357\n",
       "...      ...   ...    ...    ...\n",
       "53910   0.70     E   60.5   2753\n",
       "53911   0.57     E   59.8   2753\n",
       "53912   0.61     F   61.8   2753\n",
       "53913   0.80     G   64.2   2753\n",
       "53914   0.84     I   63.7   2753\n",
       "53915   0.77     E   62.1   2753\n",
       "53916   0.74     D   63.1   2753\n",
       "53917   0.90     J   63.2   2753\n",
       "53918   0.76     I   59.3   2753\n",
       "53919   0.76     I   62.2   2753\n",
       "53920   0.70     E   62.4   2755\n",
       "53921   0.70     E   62.8   2755\n",
       "53922   0.70     D   63.1   2755\n",
       "53923   0.73     I   61.3   2756\n",
       "53924   0.73     I   61.6   2756\n",
       "53925   0.79     I   61.6   2756\n",
       "53926   0.71     E   61.9   2756\n",
       "53927   0.79     F   58.1   2756\n",
       "53928   0.79     E   61.4   2756\n",
       "53929   0.71     G   61.4   2756\n",
       "53930   0.71     E   60.5   2756\n",
       "53931   0.71     F   59.8   2756\n",
       "53932   0.70     E   60.5   2757\n",
       "53933   0.70     E   61.2   2757\n",
       "53934   0.72     D   62.7   2757\n",
       "53935   0.72     D   60.8   2757\n",
       "53936   0.72     D   63.1   2757\n",
       "53937   0.70     D   62.8   2757\n",
       "53938   0.86     H   61.0   2757\n",
       "53939   0.75     D   62.2   2757\n",
       "\n",
       "[53940 rows x 4 columns]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17561"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#问题一\n",
    "data = df.query('carat>1')['price']\n",
    "data.max()-data.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "#问题二\n",
    "#第一步 进行分组 根据百分比进行分组 不是根据某个实数值进行分组\n",
    "bins = df['depth'].quantile(np.linspace(0,1,6)).tolist()\n",
    "cuts = pd.cut(df['depth'],bins=bins)\n",
    "df['cuts'] = cuts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>unique</th>\n",
       "      <th>top</th>\n",
       "      <th>freq</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>cuts</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>(43.0, 60.8]</th>\n",
       "      <td>11294</td>\n",
       "      <td>7</td>\n",
       "      <td>E</td>\n",
       "      <td>2259</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(60.8, 61.6]</th>\n",
       "      <td>11831</td>\n",
       "      <td>7</td>\n",
       "      <td>G</td>\n",
       "      <td>2593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(61.6, 62.1]</th>\n",
       "      <td>10403</td>\n",
       "      <td>7</td>\n",
       "      <td>G</td>\n",
       "      <td>2247</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(62.1, 62.7]</th>\n",
       "      <td>10137</td>\n",
       "      <td>7</td>\n",
       "      <td>G</td>\n",
       "      <td>2193</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(62.7, 79.0]</th>\n",
       "      <td>10273</td>\n",
       "      <td>7</td>\n",
       "      <td>G</td>\n",
       "      <td>2000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              count unique top  freq\n",
       "cuts                                \n",
       "(43.0, 60.8]  11294      7   E  2259\n",
       "(60.8, 61.6]  11831      7   G  2593\n",
       "(61.6, 62.1]  10403      7   G  2247\n",
       "(62.1, 62.7]  10137      7   G  2193\n",
       "(62.7, 79.0]  10273      7   G  2000"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('cuts')['color'].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [],
   "source": [
    "#该种颜色是组内平均而言单位重量最贵的吗\n",
    "#思路 先cuts and color分组 算出均重 然后再按cuts分组 得到最大的值\n",
    "df['均重价格']=df['price']/df['carat']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "cuts          color\n",
       "(43.0, 60.8]  D        4096.138305\n",
       "              E        3929.625897\n",
       "              F        4136.841550\n",
       "              G        4295.283909\n",
       "              H        4275.933161\n",
       "              I        4452.613168\n",
       "              J        3984.268069\n",
       "(60.8, 61.6]  D        3920.028256\n",
       "              E        3695.210838\n",
       "              F        4125.295966\n",
       "              G        4119.503438\n",
       "              H        3882.091164\n",
       "              I        3808.024196\n",
       "              J        3911.070010\n",
       "(61.6, 62.1]  D        3955.128801\n",
       "              E        3661.572571\n",
       "              F        4152.283895\n",
       "              G        4040.155790\n",
       "              H        3887.556283\n",
       "              I        3840.712944\n",
       "              J        3787.725141\n",
       "(62.1, 62.7]  D        3928.165051\n",
       "              E        3929.214233\n",
       "              F        4204.842133\n",
       "              G        4258.155489\n",
       "              H        4058.491162\n",
       "              I        4020.271378\n",
       "              J        3912.595178\n",
       "(62.7, 79.0]  D        3842.700843\n",
       "              E        3816.721165\n",
       "              F        4050.174162\n",
       "              G        4106.310203\n",
       "              H        3950.317729\n",
       "              I        3868.220357\n",
       "              J        3571.133086\n",
       "Name: 均重价格, dtype: float64"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(['cuts','color'])['均重价格'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([(Interval(43.0, 60.8, closed='right'), 'I'),\n",
       "       (Interval(60.8, 61.6, closed='right'), 'F'),\n",
       "       (Interval(61.6, 62.1, closed='right'), 'F'),\n",
       "       (Interval(62.1, 62.7, closed='right'), 'G'),\n",
       "       (Interval(62.7, 79.0, closed='right'), 'G')], dtype=object)"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(['cuts','color'])['均重价格'].mean().groupby(['cuts']).idxmax().values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['I', 'F', 'F', 'G', 'G']"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i[1] for i in df.groupby(['cuts','color'])['均重价格'].mean().groupby(['cuts']).idxmax().values]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>carat</th>\n",
       "      <th>color</th>\n",
       "      <th>depth</th>\n",
       "      <th>price</th>\n",
       "      <th>cuts</th>\n",
       "      <th>均重价格</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.23</td>\n",
       "      <td>E</td>\n",
       "      <td>61.5</td>\n",
       "      <td>326</td>\n",
       "      <td>(0.0, 0.5]</td>\n",
       "      <td>1417.391304</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.21</td>\n",
       "      <td>E</td>\n",
       "      <td>59.8</td>\n",
       "      <td>326</td>\n",
       "      <td>(0.0, 0.5]</td>\n",
       "      <td>1552.380952</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.23</td>\n",
       "      <td>E</td>\n",
       "      <td>56.9</td>\n",
       "      <td>327</td>\n",
       "      <td>(0.0, 0.5]</td>\n",
       "      <td>1421.739130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.29</td>\n",
       "      <td>I</td>\n",
       "      <td>62.4</td>\n",
       "      <td>334</td>\n",
       "      <td>(0.0, 0.5]</td>\n",
       "      <td>1151.724138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.31</td>\n",
       "      <td>J</td>\n",
       "      <td>63.3</td>\n",
       "      <td>335</td>\n",
       "      <td>(0.0, 0.5]</td>\n",
       "      <td>1080.645161</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   carat color  depth  price        cuts         均重价格\n",
       "0   0.23     E   61.5    326  (0.0, 0.5]  1417.391304\n",
       "1   0.21     E   59.8    326  (0.0, 0.5]  1552.380952\n",
       "2   0.23     E   56.9    327  (0.0, 0.5]  1421.739130\n",
       "3   0.29     I   62.4    334  (0.0, 0.5]  1151.724138\n",
       "4   0.31     J   63.3    335  (0.0, 0.5]  1080.645161"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#问题三\n",
    "bins=[0,0.5,1,1.5,2.2,np.inf]\n",
    "df['cuts'] = pd.cut(df['carat'],bins = bins)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当颜色为D时，截距项为：-2361.017152，回归系数为：8408.353126\n",
      "当颜色为E时，截距项为：-2381.049600，回归系数为：8296.212783\n",
      "当颜色为F时，截距项为：-2665.806191，回归系数为：8676.658344\n",
      "当颜色为G时，截距项为：-2575.527643，回归系数为：8525.345779\n",
      "当颜色为H时，截距项为：-2460.418046，回归系数为：7619.098320\n",
      "当颜色为I时，截距项为：-2878.150356，回归系数为：7761.041169\n",
      "当颜色为J时，截距项为：-2920.603337，回归系数为：7094.192092\n"
     ]
    }
   ],
   "source": [
    "#问题四 分组计算回归系数 name代表的是分组的依据 group代表的是分组之后的每个小组\n",
    "for name,group in df[['carat','price','color']].groupby('color'):\n",
    "    L1 = np.array([np.ones(group.shape[0]),group['carat']]).reshape(2,group.shape[0])\n",
    "    L2 = group['price']\n",
    "    result = (np.linalg.inv(L1.dot(L1.T)).dot(L1)).dot(L2).reshape(2,1)\n",
    "    print('当颜色为%s时，截距项为：%f，回归系数为：%f'%(name,result[0],result[1]))\n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
